/*
 * Synchronizer.cpp
 *
 *  Created on: 26/11/2008
 *      Author: fernando
 */

#include "Synchronizer.h"
#include <ctime>

namespace core {

Synchronizer::Synchronizer(Synchronizable& process) {
	_process = &process;
	_interval = 0.05;
	_stop = false;
}

Synchronizer::Synchronizer(Synchronizable& process, int cycles_per_second) {
	_process = &process;
	_interval = 1.0 / cycles_per_second; // 1 segundo dividido em x partes
	_stop = false;
}

Synchronizer::~Synchronizer() {
	_process = NULL;
}

void Synchronizer::run() {
	clock_t next_clock;
	_stop = false;
	do {
		next_clock = clock() + (_interval * CLOCKS_PER_SEC);
		_process->synch(_interval);
		while (clock() < next_clock)
			;
	} while (!_stop);
}

void Synchronizer::stop() {
	_stop = true;
}

}
